//
// Created by Administrator on 2023/11/21.
//

#include <bits/stdc++.h>
using namespace std;

const int N = 1e3 + 10;

struct Student {
    string name;
    int grade;

    bool operator<(const Student &M) const {
        return grade < M.grade;
    }

    bool operator>(const Student &M) const {
        return grade > M.grade;
    }
} student[N];

int main() {
    int n, ch;
    cin >> n>>ch;

    for (int i = 0; i < n; ++i) {
        cin >> student[i].name >> student[i].grade;
    }
    if(ch){
        sort(student,student+n);//从小到大
    }else{
        sort(student,student+n,greater<Student>());//从大到小
    }
    for (int i = 0; i < n; ++i) {
        cout<<student[i].name<<" "<<student[i].grade<<endl;
    }

}